package com.JavaNet.chapter8_client_socket;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author : wushikai
 *         <p>
 *         date : 2024-06-06
 */
public class CallBaidu {

    public static void main(String[] args) {
        Socket socket;
        try {
            socket = new Socket("baidu.com", 80); //todo 光是这样不行的,  仅仅是组成一个 tcp 报文, 而不是一个 http 报文,  tcp 之上不知道你是什么协议, 直接报错了;
            System.out.println(args[0] + "已连接成功!");


            // 从服务端程序接收数据
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String s = "";
            while ((s = bufferedReader.readLine()) != null) {

                System.out.println(s);
            }
            socket.close();
        } catch (Exception e) {
            System.err.println("错误信息：" + e.getMessage());
        }
    }
}


class TestServer {
    //    public final static String HOST = "blog.csdn.net";
//    public final static String PATH = "/yilip";
    public final static String HOST = "blog.csdn.net";
    public final static String PATH = "zmx729618/article/details/51374486";
    //    public final static String HOST = "aipboss.allinpay.com";
//    public final static String PATH = "/know/devhelp/index.php";
    public final static int PORT = 443;
    public final static int PEOPLE_NUM = 1 * 1;
    public static int num = 0;

    /**
     * Author: Lip
     */
    public static void main(String[] args) {
        for (int i = 0; i < PEOPLE_NUM; i++) {
//            System.out.println("有多少人:" + num);

            ClientThread thread = new ClientThread(true);
            thread.start();
        }
    }

}

class ClientThread extends Thread {
    Socket socket = null;
    BufferedReader br = null;
    PrintWriter pw = null;
    boolean isout;//是否输出返回结果

    public ClientThread(boolean isout) {
        this.isout = isout;
    }

    public void run( ) {


        System.setProperty("http.proxyHost", "127.0.0.1");
        System.setProperty("http.proxyPort", "8888");


        TestServer.num++;
        try {
            this.sleep(1 * 1000);
//            socket = new Socket( InetAddress.getByName(TestServer.HOST),TestServer.PORT);
              socket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(InetAddress.getByName(TestServer.HOST), TestServer.PORT);

            // 读取服务器数据
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            pw = new PrintWriter((new OutputStreamWriter(socket.getOutputStream())));
            StringBuffer requestHeader = new StringBuffer();
            requestHeader
                    .append("GET " + TestServer.PATH + "  HTTP/1.1\n")
                    .append("HOST:" + TestServer.HOST + "\n")
                    .append("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n")
                    .append("Accept-Encoding:gzip, deflate, sdch\n")
                    .append("Accept-Language:zh-CN,zh;q=0.8\n")
                    .append("Cache-Control:no-cache\n")
                    .append("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36\n")
                    .append("Encoding:UTF-8\n")
                    .append("Connection:keep-alive" + "\n")
                    .append("\n");

            if (isout) {

                System.out.println(requestHeader.toString());
            }
            pw.println(requestHeader);//
            pw.flush();
            String line = "";
            if (isout)
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            System.out.println("Get the data from  server......");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {

                System.out.println("close......");
                // br.close();
                pw.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
